package org.gurms.test;

import java.util.List;

import org.gurms.dao.po.system.SysDict;
import org.gurms.dao.po.system.SysDictPK;
import org.gurms.dao.po.system.SysLog;
import org.gurms.dao.po.system.SysMenu;
import org.gurms.dao.po.system.SysOrg;
import org.gurms.dao.po.system.SysParam;
import org.gurms.dao.po.system.SysPrivilege;
import org.gurms.dao.po.system.SysRole;
import org.gurms.dao.po.system.SysRoleMenu;
import org.gurms.dao.po.system.SysUser;
import org.gurms.dao.po.system.SysUserInfo;
import org.hibernate.CacheMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;

	static {
		try {
			sessionFactory = new AnnotationConfiguration()
					.addPackage("org.gurms.model.system")
					.addAnnotatedClass(SysUser.class)
					.addAnnotatedClass(SysUserInfo.class)
					.addAnnotatedClass(SysOrg.class)
					.addAnnotatedClass(SysLog.class)
					.addAnnotatedClass(SysRole.class)
					.addAnnotatedClass(SysDict.class)
					.addAnnotatedClass(SysMenu.class)
					.addAnnotatedClass(SysPrivilege.class)
					.addAnnotatedClass(SysParam.class)
					.addAnnotatedClass(SysRoleMenu.class)
					.addAnnotatedClass(SysDictPK.class)
					.buildSessionFactory();
		} catch (Throwable ex) {
			// Log exception!
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static Session getSession() throws HibernateException {
		return sessionFactory.openSession();
	}
	
	public static void savelog(){
		Session s = getSession();
		SysLog log = new SysLog();
		log.setMenuid("1");
		log.setOperateid(2);
		log.setUserid("001");
		log.setUserip("1.1.1");
		
		s.beginTransaction();
		s.save(log);
		s.getTransaction().commit();
	}
	
	public static void save(){
		Session s = getSession();
		s.beginTransaction();
		System.out.println("test");
		SysUserInfo userinfo = new SysUserInfo();
		userinfo.setAddress("address");
		userinfo.setBirthday("19990909");
		userinfo.setCity("sz");
		userinfo.setCompany("kingdom");
		userinfo.setEducation("2");
		userinfo.setEmail("123@123.com");
		userinfo.setHometel("0716-11111");
		userinfo.setMobile("13666666666");
		userinfo.setPostcode("434100");
		userinfo.setSex("1");
		userinfo.setUsername("黄勇华");
		userinfo.setUserid("hyh1");

		SysUser user = new SysUser();
		user.setUserid("hyh5");
		user.setUsername("黄勇华5");
		user.setLoginpassword("123");
		user.setLoginip("123.123.123.23");
		user.setLogincount(3);
		user.setLogintime("232323");
		user.setOnlineflag("1");
		user.setUserstatus("1");

		SysOrg org = new SysOrg();
		org.setFullname("orgfullname22");
		org.setLinkman("-linkman22-");
		org.setShortname("sname22");
		
		SysRole role = new SysRole();
		role.setRolename("role-d");
		role.setRolestatus("1");
		role.setCreater("creater");

//		user.setSysorg(org);
//		org.getSysusers().add(user);
//		org = (SysOrg)s.get(SysOrg.class, "b");

		user = (SysUser)s.get(SysUser.class,"hyh14");
		System.out.println(user==null);
//		user.addRole(role);
//		user.setSysorg(null);
//		s.save(user);
//		s.delete(user);
//		s.delete(org);
		s.getTransaction().commit();
	}
	
	public static void saveMany2many(){
		SysRole role1 = new SysRole();
		role1.setRoleid("role1");
		role1.setRolename("rolename1");
		
		SysRole role2 = new SysRole();
		role2.setRoleid("role2");
		role2.setRolename("rolename2");
		
		SysMenu menu1 = new SysMenu();
		menu1.setMenuid("menu1");
		menu1.setMenuname("menuname1");
		menu1.setMenuurl("url1");
		menu1.setPrivilegelist(1);
		menu1.setMenustatus("1");
		
		SysMenu menu2 = new SysMenu();
		menu2.setMenuid("menu2");
		menu2.setMenuname("menuname2");
		menu2.setMenuurl("url2");
		menu2.setPrivilegelist(2);
		menu2.setMenustatus("2");
		
		
		Session s = getSession();
		s.beginTransaction();
		s.save(role1);
		s.save(role2);
		s.save(menu1);
		s.save(menu2);
		s.getTransaction().commit();
	}
	
	public static void getMany2many(String id){
		SysRole role = new SysRole();
		Session s = getSession();
		s.beginTransaction();
		s.load(role, id);
		s.getTransaction().commit();
		s.close();
		System.out.println(role.getRolename());
	}
	
	public static void delete(String id){
		SysUser user = new SysUser();
		Session s = getSession();
		s.beginTransaction();
		s.load(user, id);
		s.delete(user);
		s.getTransaction().commit();
	}
	
	public static void get(String id){
		SysUser user = new SysUser();
		Session s = getSession();
		s.beginTransaction();
		s.load(user, id);
		s.getTransaction().commit();
		System.out.println(user.getUsername());
		
		System.out.println("----------------------");
		SysUserInfo userinfo = new SysUserInfo();
		s = getSession();
		s.beginTransaction();
		s.load(userinfo, id);
		s.getTransaction().commit();
		System.out.println(userinfo.getBirthday());
	}
	
	public static void saveOrg(){
		System.out.println("-----save org----");
		SysOrg root = new SysOrg();
		SysOrg org = new SysOrg();
		org.setFullname("fullname");
		org.setLinkman("linkman");
		org.setLinktel("linktel");
		org.setOrgorder(1);
		org.setShortname("shortname");
		Session s = getSession();
		s.beginTransaction();
		s.load(root, "root");
		org.setParentorg(root);
		s.save(org);
		s.getTransaction().commit();
		System.out.println("------save end------");
	}
	
	public static void updateOrg(){
		System.out.println("-----update org----");
		
		SysOrg org = new SysOrg();
		Session s = getSession();
		s.beginTransaction();
		s.load(org, "root");
		org.setOrgstatus("1");
		org.setLinkman("update");
		s.save(org);
		s.getTransaction().commit();
		s.close();
		System.out.println(org.getOrgstatus());
		System.out.println(org.getSuborgs().size());
		System.out.println("------update end------");
	}
	
	public static void savemenu(){
		Session s = getSession();
		SysMenu menu1 = new SysMenu();
		menu1.setMenuname("menuname3");
		menu1.setLeafflag("1");
		menu1.setMenuorder(4);
		menu1.setMenustatus("1");
		menu1.setMenuurl("a/b3.html");
		menu1.setPrivilegelist(4);
		s.beginTransaction();
		SysMenu root = new SysMenu();
		s.load(root, "0001");
//		root.addMenu(menu1);
//		root.setLeafflag("1");
		menu1.setParentmenu(root);
		s.save(menu1);
		s.getTransaction().commit();
	}
	
	public static void selectMenu(){
		Session s = getSession();
		s.beginTransaction();
		SysMenu root = new SysMenu();
		s.load(root, "root");
		Hibernate.initialize(root.getSubmenus());
		s.getTransaction().commit();
		s.close();
		System.out.println(root.getSubmenus().size());
		for(SysMenu tmp : root.getSubmenus()){
			System.out.println(tmp.getMenuorder());
		}
		
	}
	
	public static void saverole(){
		Session s = getSession();
		SysRole role1 = new SysRole();
		role1.setEnddate("20080819");
		role1.setRolename("rolename2");
		role1.setStartdate("20080716");
		role1.setCreater("system");
		s.beginTransaction();
		s.save(role1);
		s.getTransaction().commit();
	}
	
	public static void saveroleuser(){
		Session s = getSession();
		s.beginTransaction();
		SysRole role1 = new SysRole();
		SysRole role2 = new SysRole();
		s.load(role1, "0");
		s.load(role2, "1");
		System.out.println("========1========");
		SysUser user = new SysUser();
		s.load(user, "hyh1");
		
//		user.addRole(role1);
//		user.addRole(role2);
		System.out.println("=========2=======");
		role1.addUser(user);
		role2.addUser(user);
		System.out.println("=========3===========");
		s.save(role1);
		s.save(role2);
//		s.save(user);
		s.getTransaction().commit();
	}
	
	public static void savesysrolemenu(){
		SysRoleMenu srm1 = new SysRoleMenu();
		SysRoleMenu srm2 = new SysRoleMenu();
		SysRoleMenu srm3 = new SysRoleMenu();
		Session s = getSession();
		s.beginTransaction();
		SysRole role = new SysRole();
		s.load(role, "0");
		
		srm1.setMenuid("a");
		srm1.setPrivilegelist(1);
		srm1.setSysrole(role);
		
		srm2.setMenuid("b");
		srm2.setPrivilegelist(2);
		srm2.setSysrole(role);
		
		srm3.setMenuid("c");
		srm3.setPrivilegelist(4);
		srm3.setSysrole(role);
		
		role.addRoleMenu(srm1);
		role.addRoleMenu(srm2);
		role.addRoleMenu(srm3);
		
		s.save(role);
		s.getTransaction().commit();
	}
	
	public static void getObject(String id,Object obj){
		Session s = getSession();
		s.beginTransaction();
		s.load(obj, id);
		s.getTransaction().commit();
		s.close();
	}
	
	public static List getList(String hql){
		List l = null;
		Session s = getSession();
		s.beginTransaction();
		l = s.createQuery(hql).list();
		s.getTransaction().commit();
		s.close();
		return l; 
	}
	
	public static void testEhcache(){
		Session s = getSession();
		SysUser user = (SysUser)s.load(SysUser.class, "hyh1");
		System.out.println(user.getUsername());
		s.setCacheMode(CacheMode.GET);
		SysUser user0 = (SysUser)s.load(SysUser.class, "1238052203109");
		System.out.println(user0.getUsername());
		s.close();
		
//		s.getSessionFactory().evict(SysUser.class,"hyh1");
		System.out.println("--------------------------");
		s = getSession();
		SysUser user1 = (SysUser)s.load(SysUser.class, "hyh1");
		System.out.println(user1.getLoginip());
		SysUser user2 = (SysUser)s.load(SysUser.class, "1238052203109");
		System.out.println(user2.getLoginip());
		s.close();
		
	}
	
	public static void main(String[] args) {
//		save();
//		delete("hyh1");
//		get("hyh1");
//		saveMany2many();
//		getMany2many("role1");
		
//		saveOrg();
//		updateOrg();
		
//		savelog();
//		savemenu();
		selectMenu();
//		saverole();
//		saveroleuser();
//		savesysrolemenu();
//		List l = getList("from SysUser user");
//		for(Object o : l){
//			((SysUser)o).setSysorg(null);
//			((SysUser)o).setSysroles(null);
//		}
//		System.out.println("========"+l.size());
//		JSONArray obj = new JSONArray(l);
//		try {
//			System.out.println(obj.toString());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		// test ehcache
//		testEhcache();

		// test fetch=subselect
//		Session s = getSession();
//		s.beginTransaction();
//		List list = s.createQuery("from SysUser").list();
//		int len = list.size();
//		System.out.println(len);
//		for(int i = 0 ; i < len ; i ++){
//			SysUser user = (SysUser)list.get(i);
//			System.out.println(i + " : " + user.getUsername());
//			int cnt = user.getSysroles().size();
//			System.out.println("roles size : " + cnt);
//			for(int j = 0 ; j < cnt ; j ++){
//				System.out.println(user.getSysroles().get(j).getRoleid());
//			}
//		}
//		s.close();
		
	}
}
